package com.thinkgsp.webapp.controller.base;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thinkgsp.jxc.model.Inventory;
import com.thinkgsp.jxc.model.Product;
import com.thinkgsp.jxc.model.ProductBusProperties;
import com.thinkgsp.jxc.model.ProductClassify;
import com.thinkgsp.jxc.model.ProductManuf;
import com.thinkgsp.jxc.model.ProductMedicineProperties;
import com.thinkgsp.jxc.persistence.mybatis.PagingResult;
import com.thinkgsp.jxc.service.ProductService;
import com.thinkgsp.sys.model.SysAccountPeriod;
import com.thinkgsp.webapp.controller.BaseController;
import com.thinkgsp.webapp.util.ExportExcel;
import com.thinkgsp.webapp.util.ExtReturn;

@Controller
@RequestMapping(value = "/base")
public class ProductController extends BaseController {
	@Autowired
	private ProductService product;
	@RequestMapping("/searchprod.do")
    @ResponseBody
    public Object searchprod(HttpServletRequest request,
			HttpServletResponse response){
		try {
			String val=request.getParameter("val");
			if(StringUtils.isBlank(val)) return new ExtReturn(false,"查询条件不能为空");
			int limit = Integer.parseInt(request.getParameter("limit"));
			int start = Integer.parseInt(request.getParameter("start"));
//			List<Product> products=null;
//			products=this.product.searchProducts(val);
//			return products;
			PagingResult<Product> result = product.searchProducts(val, start, limit);
			return result;
		} catch (Exception e) {
			return new ExtReturn(false,"查询商品资料出错:"+e.getMessage(),e);
		}
		
	}
	@RequestMapping("/getprodclassify.do")
    @ResponseBody
    public Object getProdClassfiy(HttpServletRequest request,
			HttpServletResponse response){
		try {
			return this.product.getProductClassifyTree();
		} catch (Exception e) {
			return new ExtReturn(false,"查询商品类型出错:"+e.getMessage(),e);
		}
		
	}
	@RequestMapping("/getprodbyclassify.do")
    @ResponseBody
    public Object getProdbyClassify(HttpServletRequest request,
			HttpServletResponse response){
		try {
			String classifyId=request.getParameter("classifyId");
			int limit = Integer.parseInt(request.getParameter("limit"));
			int start = Integer.parseInt(request.getParameter("start"));
			if(StringUtils.isBlank(classifyId)) return new ExtReturn(false,"查询条件不能为空");
			PagingResult<Product> products=null;
			products=this.product.getProductsByClassifyId(Integer.decode(classifyId), start, limit);
			return products;
		} catch (Exception e) {
			return new ExtReturn(false,"查询商品资料出错:"+e.getMessage(),e);
		}
		
	}
	@RequestMapping("/updprodclassify.do")
    @ResponseBody
    public Object updProdClassify(HttpServletRequest request,
			HttpServletResponse response){
		try {
			String classifyId=request.getParameter("classifyId");
			String classifyName=request.getParameter("classifyName");
			String parentId=request.getParameter("parentId");
			Integer pid=1;
			if(StringUtils.isBlank(classifyName)) return new ExtReturn(false,"商品类别名称不能为空");
			if(!StringUtils.isBlank(parentId))
				pid=Integer.decode(parentId);
			ProductClassify pc=new ProductClassify();
			if(!StringUtils.isBlank(classifyId)){//为空新增
				pc.setClassifyId(Integer.decode(classifyId));
			}
			pc.setParentId(pid);
			pc.setClassifyName(classifyName);
			product.saveProductClassify(pc);
			return new ExtReturn(true,"修改商品类别成功");
		} catch (Exception e) {
			return new ExtReturn(false,"修改商品类别出错:"+e.getMessage(),e);
		}
		
	}
	@RequestMapping(value = "/updproduct.do",method = RequestMethod.POST )
	@ResponseBody
	public Object updProduct(Model model ,@RequestBody Product product) {
		try {
			this.product.saveProduct(product);
			this.loger.debug("updated product");
			return new ExtReturn(true,"商品修改成功");
		} catch (Exception e) {
			return new ExtReturn(false,"商品修改出错:"+e.getMessage(),e);
		}	
	}
	@RequestMapping(value = "/updprodbus.do",method = RequestMethod.POST )
	@ResponseBody
	public Object updProductBusProperty(Model model ,@RequestBody ProductBusProperties bus) {
		try {
			
			this.loger.debug("update ProductBusProperty");
			return new ExtReturn(true,"商品业务属性修改成功");
		} catch (Exception e) {
			return new ExtReturn(false,"商品业务属性修改出错:"+e.getMessage(),e);
		}	
	}
	@RequestMapping(value = "/updprodmedicine.do",method = RequestMethod.POST )
	@ResponseBody
	public Object updProductMedicineProperty(Model model ,@RequestBody ProductMedicineProperties medicine) {
		try {
			this.loger.debug("update ProductMedicineProperty");
			return new ExtReturn(true,"商品药物属性修改成功");
		} catch (Exception e) {
			return new ExtReturn(false,"商品药物属性修改出错:"+e.getMessage(),e);
		}	
	}
	@RequestMapping("/getallmanuf.do")
    @ResponseBody
    public Object getAllManuf(HttpServletRequest request,
			HttpServletResponse response){
		try {
			return this.product.getAllProductManuf();
		} catch (Exception e) {
			return new ExtReturn(false,"查询生产企业资料出错",e);
		}
		
	}
	@RequestMapping(value = "/updprodmanuf.do",method = RequestMethod.POST )
	@ResponseBody
	public Object updProdManuf(Model model ,@RequestBody ProductManuf manuf) {
		try {
			this.loger.debug("update manuf");
			this.product.saveProductManuf(manuf);
			return new ExtReturn(true,"生产企业修改成功",manuf);
		} catch (Exception e) {
			return new ExtReturn(false,"生产企业修改出错:"+e.getMessage(),e);
		}	
	}
	@RequestMapping("/searchmanuf.do")
    @ResponseBody
    public Object searchManuf(HttpServletRequest request,
			HttpServletResponse response){
		try {
			String val=request.getParameter("val");
			if(StringUtils.isBlank(val)) return new ExtReturn(false,"查询条件不能为空");
			return this.product.searchProductManuf(val);
		} catch (Exception e) {
			return new ExtReturn(false,"查询生产企业出错:"+e.getMessage(),e);
		}
		
	}
	
	@RequestMapping("/getProduct.do")
    @ResponseBody
    public Object getProduct(@RequestParam Integer productId){
		try {
			return this.product.getProduct(productId);
		} catch (Exception e) {
			loger.error(e.getMessage());
			return new ExtReturn(false,"获取商品信息出错:"+e.getMessage(),e);
		}
		
	}
	@RequestMapping(value = "/delProduct.do")
	@ResponseBody
	public Object del(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			String pam=request.getParameter("ids");
			String[] ids=pam.split(",");
			for (String id : ids) {
				this.product.disableProduct(Integer.decode(id));
			}
			return new ExtReturn(true,"删除商品信息成功");
		} catch (Exception e) {
			this.loger.error(e.getMessage());
			return new ExtReturn(false,"删除商品信息出错:"+e.getMessage(),e);
		}
	}
	@RequestMapping(value = "/restoreProd.do")
	@ResponseBody
	public Object restoreProd(@RequestParam Integer productId) {
		try {
			this.product.enableProduct(productId);
			return new ExtReturn(true,"商品信息还原成功");
		} catch (Exception e) {
			this.loger.error(e);
			return new ExtReturn(false,"商品信息还原出错:"+e.getMessage(),e);
		}	
	}
	
	@RequestMapping("/recycleProd.do")
    @ResponseBody
    public Object recycleProd(HttpServletRequest request,
			HttpServletResponse response){
		try {
			return this.product.getDeletedProducts();
		} catch (Exception e) {
			return new ExtReturn(false,"查询商品回收站信息出错:"+e.getMessage(),e);
		}
		
	}
	
	@RequestMapping("/getProductXLS.do")
    public void  getXLS(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		try{
			String classifyId=request.getParameter("classifyId");
			if(StringUtils.isBlank(classifyId)) classifyId="1";
			response.setContentType("octets/stream");
	        response.addHeader("Content-Disposition","attachment;filename="+ new String(("产品数据导出").getBytes("GB2312"),"iso8859-1")+".xls");
			List<Product>  prod= this.product.getProductsByClassifyId(Integer.parseInt(classifyId));
			OutputStream out = response.getOutputStream();
	        ExportExcel<Product> ex = new ExportExcel<Product>();
	        HSSFWorkbook book= ex.exportExcelByFieldAnnotation(prod);
	        book.write(out);
		}catch(Exception e){
			loger.error(e);	
		}		 
	}
}
